package com.solace.selenium.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.solace.selenium.entity.VideoGroup;
import com.solace.selenium.entity.VideoOps;
import com.solace.selenium.enums.ConfTypeEnum;
import com.solace.selenium.enums.TaskTypeEnum;
import com.solace.selenium.mapper.VideoGroupMapper;
import com.solace.selenium.mapper.VideoOpsMapper;
import com.solace.selenium.service.VideoGroupService;
import com.solace.selenium.service.VideoOpsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Transactional
public class VideoOpsServiceImpl extends ServiceImpl<VideoOpsMapper, VideoOps> implements VideoOpsService {
    @Override
    public List<VideoOps> findByGroupId(String groupId) {
        LambdaQueryWrapper<VideoOps> wrapper = new QueryWrapper<VideoOps>()
                .lambda()
                .eq(VideoOps::getGroupId,groupId)
                .orderByAsc(VideoOps::getSort);
        return list(wrapper);
    }

    @Override
    public void delByGroup(String groupId) {
        LambdaQueryWrapper<VideoOps> wrapper = new QueryWrapper<VideoOps>()
                .lambda()
                .eq(VideoOps::getGroupId,groupId)
               ;
        remove(wrapper);
    }

    @Override
    public JSONArray getTaskType() {
        JSONArray array = new JSONArray();
        TaskTypeEnum[] values = TaskTypeEnum.values();
        for (TaskTypeEnum value : values) {
            JSONObject js = new JSONObject();
            Integer type = value.getType();
            String ops = value.getOps();
            String note = value.getNote();
            js.put("type", type);
            js.put("ops", ops);
            js.put("note", note);
            array.add(js);
        }
return array;
    }

    @Override
    public JSONArray getConfType() {
        JSONArray array = new JSONArray();
        ConfTypeEnum[] values = ConfTypeEnum.values();
        for (ConfTypeEnum value : values) {
            JSONObject js = new JSONObject();
            Integer type = value.getType();
            String name = value.getName();
            String note = value.getNote();
            js.put("type", type);
            js.put("name", name);
            js.put("note", note);
            array.add(js);
        }
        return array;
    }

    @Override
    public void checkData(List<VideoOps> list) {
        if (!CollectionUtils.isEmpty(list)){
            for (VideoOps videoOps : list) {
                Assert.isTrue(!(videoOps.getHours()==null&& videoOps.getMinutes()==null&&  videoOps.getSeconds()==null),"间隔时间不能为空");
                Assert.hasText(videoOps.getGroupId(),"分组ID不能为空");
                String ops = videoOps.getOps();
                Assert.hasText(ops,"操作类型不能为空");
                if(!"间隔".equals(ops)){
                    Assert.hasText(videoOps.getGoodsId(),"商品ID不能为空");
                }
                if("序号".equals(ops)){
                    Assert.hasText(videoOps.getGoodsNumber(),"商品序号不能为空");
                }
                if (videoOps.getHours()==null){
                    videoOps.setHours(0);
                }
                if (videoOps.getMinutes()==null){
                    videoOps.setMinutes(0);
                }
                if (videoOps.getSeconds()==null){
                    videoOps.setHours(0);
                }
            }
        }
    }
}